﻿using Microsoft.Win32;
using NetworkPower.DataModel.Models;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace NetworkPower.UI
{
    /// <summary>
    /// PreviewReport.xaml 的交互逻辑
    /// </summary>
    public partial class PreviewReport : Window
    {
        static NetworkPowerContext context = new NetworkPowerContext();
        public PreviewReport()
        {
            InitializeComponent();
            LoadPreviewData();
        }

        private void LoadPreviewData()
        {
            //List<ElectricCable> datasource = new List<ElectricCable>();
            //var dbElectricCables = context.ElectricCables.OrderBy(etc => etc.UnitId).ToList().GroupBy(u => u.UnitId, u => u);
            //foreach (var item in dbElectricCables)
            //{
            //    List<ElectricCable> sl = item.ToList<ElectricCable>();
            //    for (int i = 0; i < sl.Count(); i++)
            //    {
            //        ElectricCable electriCable = new ElectricCable();
            //        var dbElectec = sl[i];
            //        if (i > 0)
            //        {
            //            electriCable.ElectricCableId = dbElectec.ElectricCableId;
            //            electriCable.UnitId = Guid.Empty;
            //            electriCable.CableNumber = string.Empty;
            //            electriCable.CableType = string.Empty;
            //            electriCable.BackUpCoreCount = -1;
            //            electriCable.StartCircleNo = dbElectec.StartCircleNo;
            //            electriCable.StartTerminalNo = dbElectec.StartTerminalNo;
            //            electriCable.Start = string.Empty;
            //            electriCable.EndCircleNo = dbElectec.EndCircleNo;
            //            electriCable.EndTerminalNo = dbElectec.EndTerminalNo;
            //            electriCable.End = string.Empty;
            //            electriCable.LineLength = dbElectec.LineLength;
            //            electriCable.Remarks = dbElectec.Remarks;
            //        }
            //        else
            //        {
            //            electriCable.ElectricCableId = dbElectec.ElectricCableId;
            //            electriCable.UnitId = dbElectec.UnitId;
            //            electriCable.CableNumber = dbElectec.CableNumber;
            //            electriCable.CableType = dbElectec.CableType;
            //            electriCable.BackUpCoreCount = dbElectec.BackUpCoreCount;
            //            electriCable.StartCircleNo = dbElectec.StartCircleNo;
            //            electriCable.StartTerminalNo = dbElectec.StartTerminalNo;
            //            electriCable.Start = dbElectec.Start;
            //            electriCable.EndCircleNo = dbElectec.EndCircleNo;
            //            electriCable.EndTerminalNo = dbElectec.EndTerminalNo;
            //            electriCable.End = dbElectec.End;
            //            electriCable.LineLength = dbElectec.LineLength;
            //            electriCable.Remarks = dbElectec.Remarks;
            //        }
            //        datasource.Add(electriCable);
            //    }
            //}
            var dbElectricCables = context.ElectricCables.OrderBy(etc => etc.UnitId).Distinct().ToList();
            this.dataGrid.ItemsSource = dbElectricCables;
        }

        private void btnPreview_Click(object sender, RoutedEventArgs e)
        {
            bool? isPort = false;
            bool? isNumber = false;
            bool? isCore = false;
            var option = new ReportOption();
            option.ShowDialog();
            isPort = option.isPort;
            isNumber = option.isNumber;
            isCore = option.isCore;
            if ((isNumber.HasValue && isNumber.Value) || (isPort.HasValue && isPort.Value))
            {
                ExportExecl(context.ElectricCables.ToList(), isPort, isNumber, isCore);
            }
            else
            {
                List<ElectricCable> cabelSource = new List<ElectricCable>();
                IEnumerable<IGrouping<string, ElectricCable>> query = context.ElectricCables.GroupBy(w => w.CableNumber, w => w);
                foreach (IGrouping<string, ElectricCable> item in query)
                {
                    List<ElectricCable> sl = item.ToList<ElectricCable>();
                    cabelSource.Add(sl.FirstOrDefault());
                }
                ExportExecl(cabelSource, isPort, isNumber, isCore);
            }
        }

        public void ExportExecl(List<ElectricCable> electricCables, bool? isPort, bool? isNumber, bool? isCore)
        {
            int PageSise = 24;// the blank row and header of column
            HSSFWorkbook workbook = new HSSFWorkbook();
            int sheetCount = (int)Math.Ceiling(electricCables.Count / 24.0);

            for (int i = 0; i < sheetCount; i++)
            {
                List<ElectricCable> sheetSource = electricCables.Skip(i * PageSise).Take(PageSise).ToList(); //one sheet
                HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet((i + 1).ToString());
                SetColumnHeaderRow(workbook, sheet, isPort, isNumber, isCore);
                SetCommonRows(workbook, sheet, sheetSource, isPort, isNumber, isCore);
            }
            SaveByOpenDialog(workbook);
        }

        private void SaveByOpenDialog(HSSFWorkbook workbook)
        {
            string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = "电缆清册";
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
            using (FileStream fs = new FileStream(saveFileName, FileMode.Create))
            {
                workbook.Write(fs);
                MessageBox.Show("导出完成！");
            }
        }

        private void SaveToDefaultFolder(HSSFWorkbook workbook)
        {
            string filePath = "F:/test.xls";
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                workbook.Write(fs);
                MessageBox.Show("导出完成！");
            }
        }

        private void SetCommonRows(HSSFWorkbook workbook, ISheet sheet, List<ElectricCable> sheetSource, bool? isPort, bool? isNumber, bool? isCore)
        {
            IEnumerable<IGrouping<Guid, ElectricCable>> query = sheetSource.GroupBy(g => g.UnitId, g => g); //group by Installed company
            int RowIndex = 2;// Except the blank row and the header of column
            int gloableRow = 2;
            foreach (IGrouping<Guid, ElectricCable> info in query)
            {
                List<ElectricCable> sl = info.ToList<ElectricCable>();
                var groupedElectricCable = sl.GroupBy(e => e.CableNumber, e => e); //再以电缆编号分组
                foreach (IGrouping<string, ElectricCable> item in groupedElectricCable)  //那么每一组都是包含了几根匹配的线，它们的电缆编号是一样的
                {
                    List<ElectricCable> Ecc = item.OrderBy(e => e.CableNumber).ToList();//取到一个组内的所有匹配上的电缆
                    int startRowOfMerge = RowIndex;//下一个标识符，目的是为了保存住需要合并的单元格的初始行标
                    if ((isPort.HasValue && isPort.Value) || (isNumber.HasValue && isNumber.Value))
                    {
                        for (int n = 0; n < Ecc.Count(); n++)
                        {
                            ElectricCable electricCable = Ecc[n];
                            IRow row = sheet.CreateRow(RowIndex);
                            ICell cell = row.CreateCell(0);

                            var unit = context.Units.Where(u => u.UnitID == electricCable.UnitId).FirstOrDefault();

                            cell.SetCellValue(unit.UnitName); SetCommonRowCellStyle(workbook, cell, false);
                            sheet.SetColumnWidth(0, 20 * 256);

                            int CurrentColumnIndex = 1;
                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.CableNumber);
                            SetCommonRowCellStyle(workbook, cell);
                            sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                            CurrentColumnIndex++;

                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.CableType);
                            SetCommonRowCellStyle(workbook, cell, true, true);
                            sheet.SetColumnWidth(CurrentColumnIndex, 25 * 256);
                            CurrentColumnIndex++;

                            if (isCore.HasValue && isCore.Value)
                            {
                                cell = row.CreateCell(CurrentColumnIndex);
                                cell.SetCellValue(electricCable.BackUpCoreCount);
                                SetCommonRowCellStyle(workbook, cell);
                                sheet.SetColumnWidth(CurrentColumnIndex, 8 * 256);
                                CurrentColumnIndex++;
                            }

                            if (isNumber.HasValue && isNumber.Value)
                            {
                                cell = row.CreateCell(CurrentColumnIndex);
                                cell.SetCellValue(electricCable.StartCircleNo);
                                SetCommonRowCellStyle(workbook, cell);
                                sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                                CurrentColumnIndex++;
                            }

                            if (isPort.HasValue && isPort.Value)
                            {
                                cell = row.CreateCell(CurrentColumnIndex);
                                cell.SetCellValue(electricCable.StartTerminalNo);
                                SetCommonRowCellStyle(workbook, cell);
                                sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                                CurrentColumnIndex++;
                            }

                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.Start);
                            SetCommonRowCellStyle(workbook, cell, true, true);
                            sheet.SetColumnWidth(CurrentColumnIndex, 35 * 256);
                            CurrentColumnIndex++;

                            if (isNumber.HasValue && isNumber.Value)
                            {
                                cell = row.CreateCell(CurrentColumnIndex);
                                cell.SetCellValue(electricCable.EndCircleNo);
                                SetCommonRowCellStyle(workbook, cell);
                                sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                                CurrentColumnIndex++;
                            }

                            if (isPort.HasValue && isPort.Value)
                            {
                                cell = row.CreateCell(CurrentColumnIndex);
                                cell.SetCellValue(electricCable.StartTerminalNo);
                                SetCommonRowCellStyle(workbook, cell);
                                sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                                CurrentColumnIndex++;
                            }

                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.End);
                            SetCommonRowCellStyle(workbook, cell, true, true);
                            sheet.SetColumnWidth(CurrentColumnIndex, 35 * 256);
                            CurrentColumnIndex++;

                            cell = row.CreateCell(CurrentColumnIndex);
                            //cell.SetCellValue(electricCable.LineLength.ToString());
                            SetCommonRowCellStyle(workbook, cell);
                            sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                            CurrentColumnIndex++;

                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.Remarks);
                            SetCommonRowCellStyle(workbook, cell);
                            sheet.SetColumnWidth(CurrentColumnIndex, 12 * 256);
                            CurrentColumnIndex++;

                            RowIndex++;
                            gloableRow++;
                        }

                        if ((isNumber.HasValue && isNumber.Value) || (isPort.HasValue && isPort.Value))
                        {
                            SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 1, 1);
                            SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 2, 2);
                        }

                        if ((isNumber.HasValue && isNumber.Value) && (isPort.HasValue && isPort.Value)) //端子号和编号都选了
                        {
                            if (isCore.HasValue && isCore.Value) //因为备用芯也要占一列
                            {
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 6, 6);
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 9, 9);
                            }
                            else
                            {
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 5, 5);
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 8, 8);
                            }
                        }
                        else if ((isNumber.HasValue && isNumber.Value) || (isPort.HasValue && isPort.Value)) //如果其中有一个选择了
                        {
                            if (isCore.HasValue && isCore.Value) //因为备用芯也要占一列
                            {
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 5, 5);
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 7, 7);
                            }
                            else
                            {
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 4, 4);
                                SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 6, 6);
                            }
                        }

                        if (isCore.HasValue && isCore.Value)
                        {
                            SetCellRangeAddress(sheet, startRowOfMerge, RowIndex - 1, 3, 3);
                        }
                    }
                    else
                    {
                        ElectricCable electricCable = Ecc.FirstOrDefault();
                        IRow row = sheet.CreateRow(RowIndex);
                        ICell cell = row.CreateCell(0);

                        var unit = context.Units.Where(u => u.UnitID == electricCable.UnitId).FirstOrDefault();

                        cell.SetCellValue(unit.UnitName); SetCommonRowCellStyle(workbook, cell, false);
                        sheet.SetColumnWidth(0, 20 * 256);

                        int CurrentColumnIndex = 1;
                        cell = row.CreateCell(CurrentColumnIndex);
                        cell.SetCellValue(electricCable.CableNumber);
                        SetCommonRowCellStyle(workbook, cell);
                        sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                        CurrentColumnIndex++;

                        cell = row.CreateCell(CurrentColumnIndex);
                        cell.SetCellValue(electricCable.CableType);
                        SetCommonRowCellStyle(workbook, cell, true, true);
                        sheet.SetColumnWidth(CurrentColumnIndex, 25 * 256);
                        CurrentColumnIndex++;

                        if (isCore.HasValue && isCore.Value)
                        {
                            cell = row.CreateCell(CurrentColumnIndex);
                            cell.SetCellValue(electricCable.BackUpCoreCount);
                            SetCommonRowCellStyle(workbook, cell);
                            sheet.SetColumnWidth(CurrentColumnIndex, 8 * 256);
                            CurrentColumnIndex++;
                        }

                        cell = row.CreateCell(CurrentColumnIndex);
                        cell.SetCellValue(electricCable.Start);
                        SetCommonRowCellStyle(workbook, cell, true, true);
                        sheet.SetColumnWidth(CurrentColumnIndex, 35 * 256);
                        CurrentColumnIndex++;


                        cell = row.CreateCell(CurrentColumnIndex);
                        cell.SetCellValue(electricCable.End);
                        SetCommonRowCellStyle(workbook, cell, true, true);
                        sheet.SetColumnWidth(CurrentColumnIndex, 35 * 256);
                        CurrentColumnIndex++;

                        cell = row.CreateCell(CurrentColumnIndex);
                        //cell.SetCellValue(electricCable.LineLength.ToString());
                        SetCommonRowCellStyle(workbook, cell);
                        sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                        CurrentColumnIndex++;

                        cell = row.CreateCell(CurrentColumnIndex);
                        cell.SetCellValue(electricCable.Remarks);
                        SetCommonRowCellStyle(workbook, cell);
                        sheet.SetColumnWidth(CurrentColumnIndex, 12 * 256);
                        CurrentColumnIndex++;

                        RowIndex++;
                        gloableRow++;
                    }
                }
                if ((isNumber.HasValue && isNumber.Value) || (isPort.HasValue && isPort.Value))
                {
                    SetCellRangeAddress(sheet, gloableRow - sl.Count(), gloableRow - 1, 0, 0);
                }
                else
                {
                    SetCellRangeAddress(sheet, gloableRow - groupedElectricCable.Count(), gloableRow - 1, 0, 0);
                }
            }
        }

        public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
        {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
            sheet.AddMergedRegion(cellRangeAddress);
        }

        private void SetColumnHeaderRow(HSSFWorkbook workbook, HSSFSheet sheet, bool? isPort, bool? isNumber, bool? isCore)
        {
            int CurrentColumnIndex = 0;
            sheet.DefaultRowHeight = 20 * 20;
            IRow row0 = sheet.CreateRow(0);
            IRow row1 = sheet.CreateRow(1);

            ICell cell = row1.CreateCell(CurrentColumnIndex); SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("安装单位名称");
            sheet.SetColumnWidth(CurrentColumnIndex, 20 * 256);
            CurrentColumnIndex++;

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("电缆编号");
            sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
            CurrentColumnIndex++;

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("电缆型号及芯数×截面");
            sheet.SetColumnWidth(CurrentColumnIndex, 25 * 256);
            CurrentColumnIndex++;

            if (isCore.HasValue && isCore.Value)
            {
                cell = row1.CreateCell(CurrentColumnIndex);
                SetHeaderOfColumnCellStyle(workbook, cell);
                cell.SetCellValue("备用芯");
                sheet.SetColumnWidth(CurrentColumnIndex, 8 * 256);
                CurrentColumnIndex++;
            }
            if (isNumber.HasValue && isNumber.Value)
            {
                cell = row1.CreateCell(CurrentColumnIndex);
                SetHeaderOfColumnCellStyle(workbook, cell);
                cell.SetCellValue("回路编号");
                sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                CurrentColumnIndex++;
            }

            if (isPort.HasValue && isPort.Value)
            {
                cell = row1.CreateCell(CurrentColumnIndex);
                SetHeaderOfColumnCellStyle(workbook, cell);
                cell.SetCellValue("端子号");
                sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                CurrentColumnIndex++;
            }

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("起        点");
            sheet.SetColumnWidth(CurrentColumnIndex, 25 * 256);
            CurrentColumnIndex++;

            if (isNumber.HasValue && isNumber.Value)
            {
                cell = row1.CreateCell(CurrentColumnIndex);
                SetHeaderOfColumnCellStyle(workbook, cell);
                cell.SetCellValue("回路编号");
                sheet.SetColumnWidth(CurrentColumnIndex, 15 * 256);
                CurrentColumnIndex++;
            }

            if (isPort.HasValue && isPort.Value)
            {
                cell = row1.CreateCell(CurrentColumnIndex);
                SetHeaderOfColumnCellStyle(workbook, cell);
                cell.SetCellValue("端子号");
                sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
                CurrentColumnIndex++;
            }

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("终        点");
            sheet.SetColumnWidth(CurrentColumnIndex, 25 * 256);
            CurrentColumnIndex++;

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("长度(米)");
            sheet.SetColumnWidth(CurrentColumnIndex, 10 * 256);
            CurrentColumnIndex++;

            cell = row1.CreateCell(CurrentColumnIndex);
            SetHeaderOfColumnCellStyle(workbook, cell);
            cell.SetCellValue("备注");
            sheet.SetColumnWidth(CurrentColumnIndex, 12 * 256);
            CurrentColumnIndex++;
        }

        private void SetHeaderOfColumnCellStyle(HSSFWorkbook workbook, ICell cell)
        {
            HSSFCellStyle fCellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            HSSFFont ffont = (HSSFFont)workbook.CreateFont();
            ffont.FontHeight = 15 * 15;
            ffont.FontName = "宋体";
            ffont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            ffont.Color = HSSFColor.Black.Index;
            fCellStyle.SetFont(ffont);
            fCellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直对齐
            fCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//水平对齐
            fCellStyle.BorderBottom = BorderStyle.Thin;
            fCellStyle.BorderLeft = BorderStyle.Thin;
            fCellStyle.BorderRight = BorderStyle.Thin;
            fCellStyle.BorderTop = BorderStyle.Thin;
            cell.CellStyle = fCellStyle;
        }

        private void SetCommonRowCellStyle(HSSFWorkbook workbook, ICell cell, bool isNeedShowBoarder = true, bool leftAlign = false)
        {
            HSSFCellStyle fCellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            HSSFFont ffont = (HSSFFont)workbook.CreateFont();
            ffont.FontHeight = 15 * 15;
            ffont.FontName = "宋体";
            ffont.Color = HSSFColor.Black.Index;
            fCellStyle.SetFont(ffont);
            fCellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直对齐
            if (leftAlign)
            {
                fCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;//水平对齐
            }
            else
            {
                fCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//水平对齐
            }
            if (isNeedShowBoarder)
            {
                fCellStyle.BorderBottom = BorderStyle.Thin;
                fCellStyle.BorderLeft = BorderStyle.Thin;
                fCellStyle.BorderRight = BorderStyle.Thin;
                fCellStyle.BorderTop = BorderStyle.Thin;
            }
            else
            {
                fCellStyle.BorderTop = BorderStyle.Thin;
                fCellStyle.BorderBottom = BorderStyle.Thin;
            }
            cell.CellStyle = fCellStyle;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (e.Row.Item != null)
            {
                var selectedElectricCable = e.Row.Item as ElectricCable;
                if (selectedElectricCable != null)
                {
                    var dbElectricCalbe = context.ElectricCables.Where(u => u.ElectricCableId == selectedElectricCable.ElectricCableId).FirstOrDefault();
                    if (selectedElectricCable != null)
                    {
                        System.Windows.Controls.TextBox t = e.EditingElement as System.Windows.Controls.TextBox;
                        DataGridColumn dgc = e.Column;
                        if (dgc.DisplayIndex == 1)
                        {
                            dbElectricCalbe.CableNumber = t.Text;
                        }
                        else if (dgc.DisplayIndex == 2)
                        {
                            dbElectricCalbe.CableType = t.Text;
                        }
                        context.ElectricCables.Attach(dbElectricCalbe);
                        context.Entry(dbElectricCalbe).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                }
            }
        }
    }

    public class UnitNameValueConvert : IValueConverter
    {
        private NetworkPowerContext context = new NetworkPowerContext();
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = string.Empty;
            if (value != null)
            {
                Guid unitId = new Guid(value.ToString());
                result = GetUnitName(unitId);
            }
            return result;
        }

        private string GetUnitName(Guid UnitId)
        {
            var unit = context.Units.Where(u => u.UnitID == UnitId).FirstOrDefault();
            if (unit != null)
            {
                return unit.UnitName;
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BackupCoreValueConvert : IValueConverter
    {
        private NetworkPowerContext context = new NetworkPowerContext();
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = string.Empty;
            if (value.ToString() != "-1")
            {
                result = value.ToString();
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
